
const baseUrl = "https://api-hmugo-web.itheima.net/api/public/v1/"
let ajaxConter = 0
const request = function (opations) {
      ajaxConter++
      wx.showLoading({
        title: '正在加载',
        mask:true
      })
      opations.url = baseUrl + opations.url;
      return new Promise((resolve, reject) => {
            wx.request({
                  ...opations,
                  timeout: 5000,
                  success: (result) => {
                        resolve(result)
                  },
                  fail: (err) => {
                        reject(err)
                  },
                  complete:()=>{
                        ajaxConter--
                        if(ajaxConter === 0){
                              wx.hideLoading()
                        }
                  }
            })
      })
}

const homeApi = {
      getSwiperData: function(){
            return request({
                  url: "home/swiperdata"
            })
      },
      getCatitems: function(){
            return request({
                  url: "home/catitems"
            })
      },
      getFloordata: function(){
            return request({
                  url: "home/floordata"
            })
      }
}
const categoriesApi = {
      getCategories: function(){
            return request({
                  url: "categories"
            })
      }
};
const goodsApi = {
      getGoodsList:function(data){
            return request({
                  url: "goods/search",
                  data:data
            })
      },
      getGoodsDetail:function(data){
            return request({
                  url: "goods/detail",
                  data:data
            })
      }
};
const API = {
      homeApi,
      categoriesApi,
      goodsApi
}

export default API